<?php
        session_start();
        $result_connect = 0;
        $db = array(
                'host' => '',
                'db' => '',
                'user' => '',
                'pass' => ''
        );
        $app = array( 
                'version' => '1.0.0a',
                'author' => 'Antony'
        );
        
        function connect($db) {
                try{
                        $return = mysql_connect($db['host'], $db['user'], $db['pass']);
                        mysql_select_db($db['db']);
                }catch(Exception $e) {
                        return false;
                }
                return $return;
        }
        
        function tokenTableList($t) {
                $r = mysql_query("SHOW COLUMNS FROM `" . $t . "`");
                $condition = '';
                
                if(mysql_num_rows($r) == 1) {
                        $d = mysql_fetch_array($r);
                        return '`' . $d[0] . '`';
                }else{
                        while($d = mysql_fetch_array($r)) {
                                $condition .= "IFNULL(`" . $d[0] . "`, ''),";
                        }
                        
                        if(substr($condition, strlen($condition)-1) == ',') {
                                $condition = substr($condition, 0, strlen($condition)-1);
                        }
                }
                
                return 'CONCAT(' . $condition . ')';
        }
        
        function entry($d, $t, $token, $desc, $parm = 0) {
                // retrieve field information
                $field_list = array();
                $r = mysql_query("SHOW COLUMNS FROM `" . $t . "`");
                while($d2 = mysql_fetch_array($r)) {
                        $field_list[$d2[0]] = $d2;
                }
                
                if(count($d) == 0) {
                        echo "INVALID DATA";
                }else{
                        if($desc != "BULK") {
                                echo "<form method=\"POST\" action=\"grapejuice.php?a=$desc&t=$t&token=$token\" >";
                        }
                        
                        //create dynamic entry
                        foreach($d as $k => $v) {
                                $tipe = $field_list[$k][1];
                                $size = $field_list[$k][2];
                                $sz = 70;
                                $description = $desc;
                                $attr = '';
                                
                                if(strpos($tipe, 'varchar') !== false) {
                                        $sz = substr($tipe, 8);
                                        $sz = substr($sz, 0, strlen($sz)-1);
                                        
                                        if($sz > 70) $sz = 0;
                                }
                                
                                if($description == 'DELETE' || $parm == '2')
                                        $attr = ' readonly="true" ';
                                        
                                echo "<label>$k</label>";
                                
                                if($desc == "BULK") $k = $k . '[]';
                                
                                switch($tipe) {
                                        case 'text' : 
                                                echo "<textarea $attr rows=\"5\" cols=\"70\" name=\"$k\">$v</textarea>";
                                                break;
                                        case 'datetime':
                                                echo "<input $attr type=\"text\" name=\"$k\" value=\"$v\" size=\"20\"/>";
                                                break;
                                        case 'date':
                                                echo "<input $attr type=\"text\" name=\"$k\" value=\"$v\" size=\"10\"/>";
                                                break;
                                        case 'time':
                                                echo "<input $attr type=\"text\" name=\"$k\" value=\"$v\" size=\"10\"/>";
                                                break;
                                        default:
                                                if($sz == 0) 
                                                        echo "<textarea $attr rows=\"5\" cols=\"70\" name=\"$k\">$v</textarea>";
                                                else
                                                        echo "<input $attr type=\"text\" name=\"$k\" value=\"$v\" size=\"$sz\"/>";
                                                        
                                                break;
                                }
                                echo "<br/>";
                        }
                        
                        echo "<br/>";
                        echo "<hr/>";
                        
                        if($desc != "BULK") {
                                echo "
                                <input type=\"submit\" value=\"$description\" name=\"gj_submit\" />&nbsp;";
                                
                                if($description != 'DELETE')
                                echo "
                                <input type=\"reset\" value=\"RESET\" />&nbsp;";
                                
                                echo "
                                <input type=\"button\" value=\"BACK\" onclick=\"window.location.href='grapejuice.php?t=$t';\" />
                                ";
                                echo "</form>";
                        }
                }
                
        }
        
        if(isset($_GET['connect'])) {
                $db['host']   = $_POST['host'];
                $db['db']     = $_POST['db'];
                $db['user']   = $_POST['user'];
                $db['pass']   = $_POST['pass'];
                
                $_SESSION['grapejuice_db'] = $db;
        }else if(isset($_GET['disconnect'])) {
                unset($_SESSION['grapejuice_db']);
        }
        
?>
<!DOCTYPE html>
<html>
        <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                <title>Grape Juice </title>
        <link rel="shortcut icon" href="" />

                <style>
                * {font-size: 10pt;font-family: 'Segoe UI',Arial,Helvetica,'San Serif';}
                html, body { margin:0;padding:0; }
                body { background: #ccc;padding: 5px;}
                label { font-weight: bold;padding-right: 20px;display: inline-block;width: 100px;vertical-align:top; }
                .content label { font-weight: bold;padding-right: 20px;display: inline-block;width: 200px;vertical-align:top; }
                .content a {color: #09f;}
                .sidebar {  overflow-y: scroll;z-index: 1000;width: 25%; position: fixed; top: auto; bottom: auto;left: 20px;margin: auto 0px;padding: 10px 10px 10px;background: #666;}
                .sidebar a { color: #fff;overflow: hidden; }
                .content { float: right; width: 68%;margin-right: 2%;min-height: 900px;position:relative }
                input[type="text"], input[type="password"], select, textarea {outline:none;border: 1px solid #999;padding:2.5px;vertical-align:top;}
                input[type="text"][readonly="true"], input[type="password"][readonly="true"], select[readonly="true"], textarea[readonly="true"] {background: #AAA;cursor:}
                input[type="submit"]{font-weight:bold}
                
                table { border-top: 1px solid #222;border-left: 1px solid #222;border-right: 1px solid #222;}
                table thead th, table tbody td { border: 0px solid #111;border-bottom: 1px solid #222;}
                table thead th { background: #555;color:#fff;padding: 5px}
                table thead th a { color: #10A;text-shadow: 1px 1px 0 #000;text-decoration: none}
                table tbody td { color:#000;padding: 2.5px 5px;}
                table {max-width:100%;background-color:transparent;border-collapse:separate;border-spacing:0px;}
                a.icon:hover {
                        background-color: #25f;
                }
                
                a.icon {
                        background-color: #29f;
                        width:            16px;
                        height:           16px;
                        display: inline-block;
                        padding: 4px;
                        background-repeat: no-repeat;
                        background-position: center center;
                }
                
                .edit {
                        background-image: url('');
                }
                
                .delete {
                        background-image: url('');
                }
                
                .footer { background: #666;color: #fff;clear:both;float: right;padding: 10px; }
                </style>
        </head>
                <div id="sidebar_id" class="sidebar">
                        <?php if(!isset($_SESSION['grapejuice_db']) && !$result_connect) { ?>
                        <form method="post" action="grapejuice.php?connect=1">
                                <label>HOST :    </label><input type="text" size="15" name="host" value="localhost"/><br/>
                                <label>USERNAME :</label><input type="text" size="15" name="user" value="root" /><br/>
                                <label>PASSWORD :</label><input type="text" size="15" name="pass" /><br/>
                                <label>DATABASE :</label><input type="text" size="15" name="db" /><br/>
                                <label></label><input type="submit" size="15" name="connect" value="CONNECT" /><br/>
                        </form>
                        <?php }else{
                                $db = $_SESSION['grapejuice_db'];
                                $result_connect = connect($db);
                        ?>
                                <div>
                                        <a href="grapejuice.php?disconnect=1">[DISCONNECT]</a>
                                </div>
                                <h2><a href="#">TABLE (<?php $q = mysql_query("SHOW TABLES");$c = mysql_num_rows($q);print $c; ?>) : </a></h2><hr/>
                        <?php 
                                while($d = mysql_fetch_array($q)) {
                                        print '<a href="grapejuice.php?t=' . $d[0] . '">' . $d[0] . '</a><br/>';
                                }
                        } ?>
                        
                        <div class="footer"><span style="color: #10A">&copy; GrapeJuice</span></div>
                </div>
                <div class="content">
                        <?php
                                if(isset($_GET['t'])) {
                                        $t = $_GET['t'];
                                        $order = isset($_GET['order']) ? $_GET['order'] : '0';
                                        $f = isset($_GET['f']) ? $_GET['f'] : "";
                                        
                                        // database
                                        if(isset($_GET['a'])) {
                                                if(isset($_POST['gj_submit'])) {
                                                        $token = $_GET['token'];
                                                        $t = $_GET['t'];
                                                        $a = $_GET['a'];
                                                                
                                                        if($a == 'EDIT') {
                                                                $q = '';
                                                                
                                                                foreach($_POST as $k => $v) {
                                                                        if($k != 'gj_submit')
                                                                                $q .= "`$k` = '$v',";
                                                                }
                                                                
                                                                $q = substr($q, 0, strlen($q)-1);
                                                                
                                                                $condition = tokenTableList($t);
                                                                $q = "UPDATE $t SET $q WHERE MD5($condition) = '$token'";
                                                                $r = mysql_query($q);
                                                                if($r) print '<script>alert("Update Success");</script>';
                                                                else print '<script>alert("Update Failure");</script>';
                                                        }elseif($a == 'DELETE') {
                                                                $condition = tokenTableList($t);
                                                                $q = "DELETE FROM `$t` WHERE MD5($condition) = '$token'";
                                                                $r = mysql_query($q);
                                                                if($r) print '<script>alert("Delete Success");</script>';
                                                                else print '<script>alert("Delete Failure");</script>';
                                                        }
                                                }
                                        }
                                        
                                        
                                        if(isset($_GET['edit']) || isset($_GET['delete']) || isset($_GET['bulk'])) {
                                                $desc = '';
                                                $bulk = isset($_GET['bulk']) ? $_GET['bulk'] : "";
                                                $option = isset($_POST['option']) ? $_POST['option'] : '0';
                                                
                                                if(isset($_GET['entry'])) $desc = 'ENTRY';
                                                else if(isset($_GET['delete'])) $desc = 'DELETE';
                                                else if(isset($_GET['edit'])) $desc = 'EDIT';
                                                else if(isset($_GET['bulk'])) $desc = 'BULK';
                                                
                                                if($option == '1') $desc = "BULK EDIT";
                                                else if($option == '2') $desc = "BULK DELETE";
                                                
                                                print '<h3><a href="?t=' . $t . '">' . strtoupper($t) . '</a> >> <a href="#">' . $desc . '</a></h3><hr/><br/>';
                                                
                                                $condition = tokenTableList($t);
                                                $token = isset($_GET['token']) ? $_GET['token'] : '';
                                                
                                                if($token != '') {
                                                        $q = mysql_query("SELECT * FROM `$t` WHERE md5(" . $condition . ") = '" . $token . "' ");
                                                        if(mysql_num_rows($q) > 0) {
                                                                $d = mysql_fetch_array($q, MYSQL_ASSOC);
                                                                
                                                                entry($d, $t, $token, $desc);
                                                        }
                                                }else{
                                                        //execute function 
                                                        if(isset($_GET['bulk_do'])) {
                                                                $bulk_do = $_GET['bulk_do'];
                                                                $token_list = $_POST['token'];
                                                                
                                                                if($bulk_do == '1') {
                                                                        $r = mysql_query("SHOW COLUMNS FROM `" . $t . "`");
                                                                        $field = array();
                                                                        if(mysql_num_rows($r) == 1) {
                                                                                $d = mysql_fetch_array($r);
                                                                                return '`' . $d[0] . '`';
                                                                        }else{
                                                                                while($d = mysql_fetch_array($r)) {
                                                                                        $field[] = $d[0];
                                                                                }
                                                                        }
                                                                        
                                                                        $q = array();
                                                                        
                                                                        //query
                                                                        foreach($field as $f) {
                                                                                for($i=0;$i<count($_POST[$f]);$i++) {
                                                                                        if(!isset($q[$i])) $q[$i] = "";
                                                                                        $q[$i] .= "`" . $f . "` = '" . $_POST[$f][$i] . "',";
                                                                                }
                                                                                
                                                                        }
                                                                        
                                                                        $check1 = count($q);
                                                                        $check = 0;
                                                                        
                                                                        for($i=0;$i<count($q);$i++) {
                                                                                $token = $token_list[$i];
                                                                                $q[$i] = substr($q[$i], 0, strlen($q[$i])-1);
                                                                                $check += mysql_query("UPDATE $t SET " . $q[$i] . " WHERE MD5($condition) = '$token'");
                                                                                
                                                                        }
                                                                        
                                                                        if($check1 == $check) print '<script>alert("Bulk Update Success");</script>';
                                                                        else print '<script>alert("Bulk Update Failure");</script>';
                                                                        
                                                                        print '<script>window.location.href="grapejuice.php?t=' . $t . '";</script>';
                                                                }else if($bulk_do == '2') {
                                                                        $check1 = count($q);
                                                                        $check = 0;
                                                                        
                                                                        for($i=0;$i<count($token_list);$i++) {
                                                                                $token = $token_list[$i];
                                                                                $check += mysql_query("DELETE FROM $t WHERE MD5($condition) = '$token'");
                                                                        }
                                                                        
                                                                        if($check1 == $check) print '<script>alert("Bulk Delete Success");</script>';
                                                                        else print '<script>alert("Bulk Delete Failure");</script>';
                                                                        
                                                                        print '<script>window.location.href="grapejuice.php?t=' . $t . '";</script>';
                                                                }
                                                        }
                                                        
                                                        //bulk mode
                                                        if(isset($_POST['token'])) {
                                                                $token = $_POST['token'];
                                                                
                                                                if(count($token) > 0) {
                                                                        echo "<form method=\"POST\" action=\"grapejuice.php?t=$t&bulk_do=$option&bulk=$bulk\">";
                                                                        
                                                                        for($i=0;$i<count($token);$i++) {
                                                                                $q = mysql_query("SELECT * FROM `$t` WHERE md5(" . $condition . ") = '" . $token[$i] . "' ");
                                                                                if(mysql_num_rows($q) > 0) {
                                                                                        $d = mysql_fetch_array($q, MYSQL_ASSOC);
                                                                                        echo "<input type=\"hidden\" name=\"token[]\" value=\"$token[$i]\" />";
                                                                                        entry($d, $t, $token, "BULK", $option);
                                                                                }
                                                                        }
                                                                        
                                                                        echo "<input type=\"submit\" value=\"$desc\" name=\"gj_submit\" />&nbsp;";
                                                                        if($option != '2')
                                                                                echo "<input type=\"reset\" value=\"RESET\" />";
                                                                        echo "
                                                                        <input type=\"button\" value=\"BACK\" onclick=\"window.location.href='grapejuice.php?t=$t';\" />
                                                                        ";
                                                                        echo "</form>";
                                                                }else{
                                                                        print '<script type="text/javascript">window.location.href="grapejuice.php?t=' . $t . '";</script>';
                                                                }
                                                        }else{
                                                                print '<script type="text/javascript">window.location.href="grapejuice.php?t=' . $t . '";</script>';
                                                        }
                                                }
                                        }else{
                                                $condition = tokenTableList($t);
                                                $opsi = "
                                                <option value=\"1\">Bulk Edit</option>
                                                <option value=\"2\">Bulk Delete</option>
                                                ";
                                                $q = mysql_query("SELECT *, md5(" . $condition . ") AS `grapejuice_token` FROM `$t` WHERE 1 " . ($f != "" ? "ORDER BY " . $f . " " . ($order == '0' ? 'ASC' : 'DESC') : ""));
                                                
                                                print '<table border="0" cellpadding="0" cellspacing="0">';
                                                
                                                $i = 0;
                                                
                                                echo "<b>$t </b> / " . number_format(mysql_num_rows($q)) . " record (s) : <br/><br/>";
                                                
                                                if(mysql_num_rows($q) > 0) {
                                                        echo "<form action=\"grapejuice.php?t=$t&bulk=1\" method=\"POST\">";
                                                        while($d = mysql_fetch_array($q, MYSQL_ASSOC)) {
                                                                $token = $d['grapejuice_token'];
                                                                
                                                                if(($i++)==0) {
                                                                        echo '<thead><tr>';
                                                                        echo '<th colspan="3" width="10px"></th>';
                                                                        foreach($d as $k => $v) {
                                                                                if($k != 'grapejuice_token')
                                                                                        echo '<th><a href="grapejuice.php?t=' . $t . '&f=' . $k . '&order=' . ($order == 0 ? '1' : '0') . '">' . $k . '</a></th>';
                                                                        }
                                                                        echo '</tr></thead>';
                                                                        echo '<tbody>';
                                                                }
                                                                
                                                                echo '<tr>';
                                                                echo "
                                                                <td><input type=\"checkbox\" name=\"token[]\" value=\"$token\" /></td>
                                                                <td><a title=\"edit\" href=\"grapejuice.php?edit=1&t=$t&token=$token\" class=\"icon edit\"></a></td>
                                                                <td><a title=\"delete\" href=\"grapejuice.php?delete=1&t=$t&token=$token\" class=\"icon delete\"></a></td>";
                                                                
                                                                foreach($d as $k => $v) {
                                                                        if($k != 'grapejuice_token') {
                                                                                echo "<td>$v</td>";
                                                                        }
                                                                }
                                                                
                                                                echo '</tr>';
                                                        }
                                                        
                                                        echo '</tbody></table>';
                                                        echo "<div style=\"float:left;margin-left: -2px;display:block\"><select name=\"option\">$opsi</select><input name=\"OkBtn\" type=\"submit\" value=\"Ok\" /></div>";
                                                }else{
                                                        echo '<td>NO RECORD</td>';
                                                        echo '<tbody>';
                                                        
                                                        echo '</tbody></table>';
                                                }
                                                echo "</form>";
                                        }
                                }
                        ?>
                </div>
        </body>
</html>